package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.BedMapper;
import cn.tedu.carehome.foundation.mapper.CheckInfoMapper;
import cn.tedu.carehome.foundation.mapper.RoomMapper;
import cn.tedu.carehome.foundation.mapper.StaffMapper;
import cn.tedu.carehome.foundation.pojo.dto.BedAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.BedUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.Bed;
import cn.tedu.carehome.foundation.pojo.entity.Room;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.service.IBedService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public  class BedServiceImpl implements IBedService {
    @Autowired
    BedMapper bedMapper;
    @Autowired
    CheckInfoMapper checkInfoMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    RoomMapper roomMapper;

    @Override
    public void addNew(BedAddNewDTO bedAddNewDTO) {
        log.debug("开始执行添加【床位】业务");
        if(bedMapper.countByFloorandRoom(bedAddNewDTO.getFloor(),bedAddNewDTO.getRoomnum(),bedAddNewDTO.getBednum())>0)
        {
            log.debug("添加床位失败,该床位已存在！");
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加床位失败,该床位已存在！");
        }
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedAddNewDTO,bed);
        bed.setEnable(0);
        bed.setManagerId(null);
        int l =  bedMapper.insert(bed);
        if(l!=1)
        {
            throw  new ServiceException(ServiceCode.ERR_UPDATE,"添加床位失败,服务器正忙,请稍后再试！");
        }
        log.debug("添加成功！");
    }

    @Override
    public List<BedAdminListVO> list(int floor) {
        log.debug("开始执行查询【床位】业务");
        return bedMapper.list(floor);
    }

    @Override
    public List<Integer> StandarFloor() {
        return bedMapper.StandarFloor();
    }

    @Override
    public void deleteByid(Long id) {
        log.debug("开始执行【删除床位】业务");
        if(bedMapper.countByid(id)==0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,非法此id!");
        }
        if(bedMapper.getStandardByid(id).getEnable()==1)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,该床位已有人入住!");
        }
        bedMapper.deleteByid(id);
        log.debug("删除成功！");
    }

    @Override
    public void deleteBycheckId(Long checkId,Long id) {
        log.debug("开始执行【退床位】业务");
        if(bedMapper.countByCheckId(checkId)==0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"退床失败,非法此id");
        }
        if(checkInfoMapper.getStandarByid(checkId)==null)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"退床失败,非法此入住登记信息");
        }
        bedMapper.deleteBycheckId(checkId);
        bedMapper.updateEnable(id);
        log.debug("退床成功！");
    }

    @Override
    public BedAdminStandardVO getStandardByid(Long id) {
        log.debug("开始执行【根据id查询床位信息】业务");
        BedAdminStandardVO bedAdminStandardVO = bedMapper.getStandardByid(id);
        if(bedAdminStandardVO ==null)
        {
            throw  new ServiceException(ServiceCode.ERR_NOT_FOUND,"查询失败,找不到此床位的信息");
        }
        log.debug("查询成功，数据为{}",bedAdminStandardVO);
         return bedAdminStandardVO;
    }

    @Override
    public BedAdminUpdateStandardVO upStandarByid(Long id) {
        log.debug("开始执行【根据id查询床位以及入住信息】业务");
        BedAdminStandardVO bedAdminStandardVO = bedMapper.getStandardByid(id);
        if(bedAdminStandardVO ==null)
        {
            throw  new ServiceException(ServiceCode.ERR_NOT_FOUND,"查询失败,找不到此床位的信息");
        }
        log.debug("查询成功");
        return bedMapper.upgetstandardByid(id);
    }

    @Override
    public  List<BedStaffVO>  staffList() {
        return bedMapper.staffList();
    }

    @Override
    public void update(BedUpdateDTO bedUpdateDTO) {
        {
            if (bedMapper.getStandardByid(bedUpdateDTO.getId()) == null) {
                log.debug("修改失败,该床位不存在");
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "修改失败,该床位不存在");
            }
        }
        {
            if(bedMapper.countByFloorandRoomNot(bedUpdateDTO.getFloor(), bedUpdateDTO.getRoomnum(), bedUpdateDTO.getBednum(),bedUpdateDTO.getId())>0)
            {
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "修改失败,修改的该床位号已存在");
            }
        }
        if(bedUpdateDTO.getCheckId()!=null) {
            {
                if (checkInfoMapper.getStandarByid(bedUpdateDTO.getCheckId()) == null) {
                    throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "修改失败,非法此入住登记信息");
                }
            }

            Bed bed = new Bed();
            BeanUtils.copyProperties(bedUpdateDTO, bed);
            bed.setManagerId(0L);
            bed.setEnable(bedUpdateDTO.getCheckId() == null ? 0 : 1);
            bedMapper.update(bed);

                if (roomMapper.countByCheckIdNotbedID(bedUpdateDTO.getId(), bedUpdateDTO.getCheckId()) > 0) {
                    throw new ServiceException(ServiceCode.ERR_CONFLICT, "入住失败,该老人已在其他床位入住");
                }
                RoomStandardVO roomStandardVO = roomMapper.getStandardByid(bedUpdateDTO.getId());
                if (roomStandardVO == null) {
                    Room room = new Room();
                    room.setBedId(bedUpdateDTO.getId());
                    room.setCheckId(bedUpdateDTO.getCheckId());
                    int l = roomMapper.insert(room);
                    if (l != 1) {
                        throw new ServiceException(ServiceCode.ERR_INSERT, "修改失败,服务器正忙,请稍后再试！");
                    }
                    return;
                }
                Room r = new Room();
                r.setId(roomStandardVO.getId());
                r.setCheckId(bedUpdateDTO.getCheckId());
                int l = roomMapper.update(r);
                if (l != 1) {
                    throw new ServiceException(ServiceCode.ERR_UPDATE, "修改失败,服务器正忙,请稍后再试！");
                }
                return;

        }



    }

}
